<?php

namespace App\Providers;

use App\Admin\Repositories\WxMenu;
use App\Lib\Storage\StorageHandler;
use App\Wen\Utils\Settings;
use App\Models\WxSearch;
use App\Wen\Utils\Utils;
use Elastic\Elasticsearch\ClientBuilder;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravel\Scout\EngineManager;
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Filesystem;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
//        $this->app->bind(ImportSourceFactory::class, MyImportSourceFactory::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        global $__MINI_GLOBAL_REQUEST_ENV__, $__MINI_GLOBAL_PAYCODE_1__;
        if($__MINI_GLOBAL_REQUEST_ENV__ == 'web'){
            // /single/410
            // /circle/30
            // /tag/75
            // /search/0/%E6%B5%8B%E8%AF%95
//            Utils::getMiniCode();
            // Utils::getMiniCode('pages/circle/list', $circle->id)
            View()->composer('shuixing/home', function ($view) {

                //热门搜索
                if(Settings::get('pc_search_hot_keywords')){
                    $hot_search_keywords = explode(',', Settings::get('pc_search_hot_keywords'));
                }else{
                    $hot_search_keywords = WxSearch::where('is_hot', 1)
                        ->get('search_content')->map(function ($value){
                            return $value->search_content;
                        })->all();
                }

                $mini_code = Settings::get('app_mini_code');
                if(preg_match('$/single/(\d+)$', $_SERVER["REQUEST_URI"], $single_id)){
                    $single_id = $single_id[1];
                    $mini_code = Utils::getMiniCode('pages/sticky/sticky', 'id='.$single_id);
                }else if(preg_match('$/circle/(\d+)$', $_SERVER["REQUEST_URI"], $circle_id)){
                    $circle_id = $circle_id[1];
                    $mini_code = Utils::getMiniCode('pages/circle/list', 'id='.$circle_id);
                }

                $view->with(['menus' => ['id'=>1, 'path'=> $_SERVER["REQUEST_URI"], 'menus'=> WxMenu::get_menus(1), 'foot_menus'=>[
                    'foot_menu_1' => ['id'=>2, 'menus' => WxMenu::get_menus(2)],
                    'foot_menu_2' => ['id'=>3, 'menus' => WxMenu::get_menus(3)]
                ] ],
                    'search'=>[
                        'hot_search_keywords' => $hot_search_keywords
                    ],
                    'mini_code' => $mini_code
                    ],);
            });
        }

        if($__MINI_GLOBAL_PAYCODE_1__){
            resolve(EngineManager::class)->extend('es', function () {
                return new ElasticSearchEngine(
                    ClientBuilder::create()
                        ->setHosts([env('ELASTICSEARCH_HOST')])
                        ->build()
                );
            });
        }

        Storage::extend('mini', function ($app, $config) {
            $adapter = new StorageHandler($config);
            return new FilesystemAdapter(new Filesystem($adapter), $adapter);
        });

        if(false && $__MINI_GLOBAL_REQUEST_ENV__ == 'api'){
            DB::listen(function ($query) {
                _logger_(__file__, __Line__, Str::replaceArray('?', $query->bindings, $query->sql));
            });
        }


        $this->observers();
    }


    public function observers(){

    }
}
